#include "XController.h"

#include <iostream>
#include <set>
using namespace std;



int main(int argc, char* argv[])
{
    // Get the connected players
    int numConnected;
    int* playersConnected = XController::getConnectedPlayers(&numConnected);
    
    // Create a controller for each player
    set<XController*> controllers;
    for(int i = 0; i < numConnected; i++)
    {
        controllers.insert(new XController(playersConnected[i]));
    }

	std::cout << "Press [BACK] to quit." << endl;
    
    bool done = false;
	while(numConnected > 0 && !done)
	{
	    numConnected = 0;  // Keep a running total of connected players
	    for(set<XController*>::iterator e = controllers.begin(); e != controllers.end(); e++)
	    {
	        XController* controller = *e;
	        
            controller->update();
            if(controller->isConnected())
            {
                numConnected++;
                if(controller->isPressed(XController::A))
                {
                    controller->vibrate(1.0f, 0);
                }

                if(controller->isPressed(XController::B))
                {
                    controller->vibrate(0, 1.0f);
                }

                if(controller->isPressed(XController::X))
                {
                    controller->vibrate(1.0f, 1.0f);
                }

                if(controller->isPressed(XController::Y))
                {
                    controller->vibrateOff();
                }

                if(controller->isPressed(XController::START))
                {
                    controller->vibrate(0.5f, 0.5f);
                }

                if(controller->isPressed(XController::LEFT_SHOULDER | XController::RIGHT_SHOULDER))
                {
                    controller->vibrate(0.5f, 0.5f);
                }

                if(controller->isPressed(XController::LEFT_THUMB | XController::RIGHT_THUMB))
                {
                    controller->vibrate(0.5f, 0.5f);
                }

                /*if(controller->isPressed(XController::DPAD_UP | XController::DPAD_DOWN | XController::DPAD_LEFT | XController::DPAD_RIGHT))
                {
                    controller->vibrate(0.5f, 0.5f);
                }*/

                if(controller->isPressed(XController::BACK))
                {
                    done = true;
                }

                if(controller->getLeftTriggerState() > 0.0f)
                {
                    controller->vibrate(controller->getLeftTriggerState(), 0);
                }
                
                if(controller->getRightTriggerState() > 0.0f)
                {
                    controller->vibrate(0, controller->getRightTriggerState());
                }
                
                XController::StickState stick = controller->getLeftStickState();
                if(stick.magnitude > 0.0f)
                {
                    controller->vibrate(stick.magnitude*stick.dirX, stick.magnitude*stick.dirY);
                }
                
                stick = controller->getRightStickState();
                if(stick.magnitude > 0.0f)
                {
                    controller->vibrate(stick.magnitude*stick.dirX, stick.magnitude*stick.dirY);
                }
                
                stick = controller->getDPadStickState();
                if(stick.magnitude > 0.0f)
                {
                    controller->vibrate(stick.magnitude*stick.dirX, stick.magnitude*stick.dirY);
                }
                
                /*stick = controller->getFaceStickState();
                if(stick.magnitude > 0.0f)
                {
                    controller->vibrate(stick.magnitude*stick.dirX, stick.magnitude*stick.dirY);
                }*/
            }
	    }
	}

    for(set<XController*>::iterator e = controllers.begin(); e != controllers.end(); e++)
    {
        delete *e;
    }
    controllers.clear();

	return 0;
}
